package me.leiusual.sharing.ui.order.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.paging.PagedListAdapter
import androidx.recyclerview.widget.DiffUtil
import me.leiusual.sharing.R
import me.leiusual.sharing.api.model.orderlist.Content

/**
 * author: leiusual
 * create on: 2019-07-31
 * description:
 */
class OrderListParentAdapter(
    private val clickListener: (Content) -> Unit
) : PagedListAdapter<Content, OrderListParentViewHolder>(DIFF_CALLBACK) {

  companion object {

    private val DIFF_CALLBACK = object : DiffUtil.ItemCallback<Content>() {

      override fun areItemsTheSame(oldItem: Content, newItem: Content): Boolean {
        return oldItem.id == newItem.id
      }

      override fun areContentsTheSame(oldItem: Content, newItem: Content): Boolean {
        return oldItem == newItem
      }
    }
  }

  override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): OrderListParentViewHolder {
    val inflater = LayoutInflater.from(parent.context)
    val itemView = inflater.inflate(R.layout.view_holder_order_list_parent, parent, false)
    return OrderListParentViewHolder(itemView, clickListener)
  }

  override fun onBindViewHolder(holder: OrderListParentViewHolder, position: Int) {
    holder.bind(getItem(position) ?: return)
  }
}